﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(ClubFixturePlayerMetaData))]
    public partial class ClubFixturePlayer
    {
        CricketClubEntities db = new CricketClubEntities();

        public void UpdateClubFixturePlayerRecords(FormCollection scorecard, Fixture fixture)
        {
            //
            //  create arrays for formcollection properties

            string[] formCollectionClubPlayerPlayerId = scorecard.GetValues("item.player_id");
            string[] formCollectionClubPlayerBatsmanNo = scorecard.GetValues("item.batsman_number");
            string[] formCollectionClubPlayerDismissalId = scorecard.GetValues("dismissal_id");
            string[] formCollectionClubPlayerOpFielderName = scorecard.GetValues("op_fielder_name");
            string[] formCollectionClubPlayerOpBowlerName = scorecard.GetValues("op_bowler_name");
            string[] formCollectionClubPlayerBattingRuns = scorecard.GetValues("item.batting_runs");
            string[] formCollectionClubPlayerBowlerNo = scorecard.GetValues("item.bowler_number");
            string[] formCollectionClubPlayerOvers = scorecard.GetValues("item.overs");
            string[] formCollectionClubPlayerMaidens = scorecard.GetValues("item.maidens");
            string[] formCollectionClubPlayerWickets = scorecard.GetValues("item.wickets");
            string[] formCollectionClubPlayerBowlingRuns = scorecard.GetValues("item.bowling_runs");
             
            for (int i = 0; i < formCollectionClubPlayerPlayerId.Length; i++)
            {
                int currentPlayerId = Int32.Parse(formCollectionClubPlayerPlayerId[i]);

                ClubFixturePlayer currentPlayer = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == fixture.fixture_id && cfp.player_id == currentPlayerId).First();

                if (formCollectionClubPlayerBatsmanNo[i] != "")
                {
                    currentPlayer.batsman_number = Int32.Parse(formCollectionClubPlayerBatsmanNo[i]);
                }

                if (formCollectionClubPlayerDismissalId[i] != "")
                {
                    currentPlayer.how_out = Int32.Parse(formCollectionClubPlayerDismissalId[i]);
                }

                if (formCollectionClubPlayerOpFielderName[i] != "")
                {
                    currentPlayer.fielder_name = formCollectionClubPlayerOpFielderName[i];
                }

                if (formCollectionClubPlayerOpBowlerName[i] != "")
                {
                    currentPlayer.bowler_name = formCollectionClubPlayerOpBowlerName[i];
                }

                if (formCollectionClubPlayerBattingRuns[i] != "")
                {
                    currentPlayer.batting_runs = double.Parse(formCollectionClubPlayerBattingRuns[i]);
                }

                if (formCollectionClubPlayerBowlerNo[i] != "")
                {
                    currentPlayer.bowler_number = Int32.Parse(formCollectionClubPlayerBowlerNo[i]);
                }

                if (formCollectionClubPlayerOvers[i] != "")
                {
                    currentPlayer.overs = double.Parse(formCollectionClubPlayerOvers[i]);
                }

                if (formCollectionClubPlayerMaidens[i] != "")
                {
                    currentPlayer.maidens = double.Parse(formCollectionClubPlayerMaidens[i]);
                }

                if (formCollectionClubPlayerWickets[i] != "")
                {
                    currentPlayer.wickets = double.Parse(formCollectionClubPlayerWickets[i]);
                }

                if (formCollectionClubPlayerBowlingRuns[i] != "")
                {
                    currentPlayer.bowling_runs = double.Parse(formCollectionClubPlayerBowlingRuns[i]);
                }

                db.ObjectStateManager.ChangeObjectState(currentPlayer, EntityState.Modified);
                db.SaveChanges();
            }
        }

    }

    public class ClubFixturePlayerMetaData
    {
        [Required(ErrorMessage = "You must select a unique player from the list:")]
        public int player_id;

        [Required]
        [Display(Name = "Batsman Number:")]
        [Range(1, 11, ErrorMessage = "Batman number must be in range 1 to 11")]
        public int batsman_number;

        [Required]
        [Display(Name = "How Out:")]
        public int how_out;

        [Display(Name = "Fielder Name:")]
        public int fielder_name;

        [Display(Name = "Bowler Name:")]
        public int bowler_name;

        [Display(Name = "Batting Runs:")]
        [Range(0, 300, ErrorMessage = "Runs scored must be greater than or equal to zero")]
        public double batting_runs;

        [Display(Name = "Bowler Number:")]
        [Range(1, 11, ErrorMessage = "Bowler number must be in the range 1 to 11")]
        public int bowler_number;

        [Display(Name = "Overs:")]
        [Range(0, 300, ErrorMessage = "Overs must be greater than or equal to zero")]
        public double overs;

        [Display(Name = "Maidens:")]
        [Range(0, 300, ErrorMessage = "Maidens must be greater than or equal to zero")]
        public double maidens;

        [Display(Name = "Bowling Runs:")]
        [Range(0, 300, ErrorMessage = "Runs must be greater than or equal to zero")]
        public double bowling_runs;

        [Display(Name = "Wickets:")]
        [Range(0, 10, ErrorMessage = "Wickets taken must be in range 1 to 10")]
        public double wickets;

        [Display(Name = "Contact:")]
        public bool contact;

        [Display(Name = "Contacted:")]
        public bool contacted;

        [Display(Name = "Confirmed")]
        public bool confirmed;

    }

}